import bpy
import ctypes
import initLIB.load_dll as dllAPI

class C_MBTENINE_地形添加_Operator(bpy.types.Operator):
	bl_idname = "object.gameengine_terrain_add"
	bl_label = "添加地形"

	def execute(self, context):
		地形网格 = bpy.data.meshes.new(name='地形网格')
		地形 = bpy.data.objects.new(name="地形", object_data=地形网格)

		context.scene.objects.link(地形)

		地形.select = True
		context.scene.objects.active = 地形

		场景名称 = ctypes.c_wchar_p(context.scene.name)
		物体名称 = ctypes.c_wchar_p(地形.name)
		t = dllAPI.E_物体类型()
		物体类型 = ctypes.c_uint32(t.e_地形)
		物体位置 = ctypes.py_object(地形.location)
		LOC层数 = ctypes.c_uint32(1)
		细分 = ctypes.c_uint32(4096)
		视距 = ctypes.c_float(16000.0)

		dllAPI.flexCAPI.f_addObj(场景名称, 物体名称, 物体类型, 视距, LOC层数, 细分, 细分, 细分 )

		return {'FINISHED'}


class C_MBTENINE_车辆添加_Operator(bpy.types.Operator):
	bl_idname = "object.gameengine_vehicle_add"
	bl_label = "添加车辆"

	def execute(self, context):
		车辆网格 = bpy.data.meshes.new(name='车辆网格')
		车辆 = bpy.data.objects.new(name="车辆", object_data=地形网格)
		context.scene.objects.link(车辆)

		车辆.select = True
		context.scene.objects.active = 车辆
		return {'FINISHED'}








class C_扩展物体添加子菜单(bpy.types.Menu):
	bl_idname = "object.Add_引擎_扩展物体"
	bl_label = "扩展物体"

	preset_subdir = "object/draw"
	preset_operator = "object.gameengine_terrain_add"
	draw = bpy.types.Menu.draw_preset

	'''def draw(self, context):
		layout = self.layout
		layout.operator('object.gameengine_terrain_add', icon='GRID')'''


def menu_draw_引擎扩展模型物体添加(self, context):
	if context.scene.render.engine == 'mbt_game_renderer' :
		self.layout.separator()
		#self.layout.menu(C_扩展物体添加子菜单.__name__, text=C_扩展物体添加子菜单.bl_label)
		#self.layout.operator_menu_enum("object.C_扩展物体添加子菜单", "Add_引擎_扩展物体", text="Select All by Type...")
		self.layout.operator('object.gameengine_terrain_add', icon='GRID')
		self.layout.operator('object.gameengine_vehicle_add', icon='OUTLINER_OB_GROUP_INSTANCE')
		self.layout.separator()


物体添加操作符类 = (
	C_MBTENINE_地形添加_Operator,
	C_MBTENINE_车辆添加_Operator,
	C_扩展物体添加子菜单,
)


def register():
	for cls in 物体添加操作符类:
		bpy.utils.register_class(cls)

	#bpy.types.INFO_MT_add.append(menu_draw_引擎扩展模型物体添加)
	bpy.types.INFO_MT_mesh_add.append(menu_draw_引擎扩展模型物体添加)

	print('menu register')


def unregister():
	for cls in 物体添加操作符类:
		bpy.utils.unregister_class(cls)

	#bpy.types.INFO_MT_add.remove(menu_draw_引擎扩展模型物体添加)
	bpy.types.INFO_MT_mesh_add.remove(menu_draw_引擎扩展模型物体添加)




